/* -----------------------------------------------------------------------------
 * PROJECT    : lwf_jaxb
 * FILE       : GenerateRandomFraction.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Apr 5, 2012  MyCodeTalks			       Created
 */

package net.lwf.jaxb.util.maths.fraction;

import java.util.List;
import java.util.StringTokenizer;


import net.lwf.common.constant.ICommonConstant;
import net.lwf.jaxb.entity.Fraction;
import net.lwf.number.util.RandomNumberGenerator;

public class GenerateRandomFraction {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String input = "2 3/4";
		int whole = 0;
		boolean onlyFraction = false;
		List<Integer> numerators = null;
		List<Integer> denomenators = null;
		StringTokenizer stk_fraction = null;
		String numerator = null;
		String denomenator = null;
		StringTokenizer stk = new StringTokenizer(input);
		int tokens = stk.countTokens();
		String aValue = null;
		if(tokens == 1) {
			aValue = stk.nextToken();
			//Check it is a fraction or a whole number
			if(aValue.indexOf(ICommonConstant.FORWARD_SLASH) == -1) {
				whole = Integer.parseInt(aValue);
			}
			else {
				onlyFraction = true;
				stk_fraction = new StringTokenizer(aValue, ICommonConstant.FORWARD_SLASH);
				numerator = stk_fraction.nextToken();
				denomenator = stk_fraction.nextToken();
				numerators = RandomNumberGenerator.generateIntsWithVariance(Integer.parseInt(numerator), 4, 3);
				denomenators = RandomNumberGenerator.generateIntsWithVariance(Integer.parseInt(denomenator), 4, 3);
			}
		}
		else {
			whole = Integer.parseInt(stk.nextToken());
		}
		
		String fraction = null;

		if(tokens > 1) {
			fraction = stk.nextToken();
			System.out.println(fraction);
			stk_fraction = new StringTokenizer(fraction, ICommonConstant.FORWARD_SLASH);
			numerator = stk_fraction.nextToken();
			denomenator = stk_fraction.nextToken();
			numerators = RandomNumberGenerator.generateIntsWithVariance(Integer.parseInt(numerator), 4, 3);
			denomenators = RandomNumberGenerator.generateIntsWithVariance(Integer.parseInt(denomenator), 4, 3);
		}
		
		List<Integer> wholes = null;
		String answer_choice = "";
		int nCounter = 0;

		if(!onlyFraction) {
			wholes = RandomNumberGenerator.generateIntsWithVariance(whole, 4, 3);
			for(Integer value: wholes) {
				answer_choice += value.toString();
				if(numerators != null && denomenators != null) {
					answer_choice += " ";
					int numerator_choice = numerators.get(nCounter).intValue();
					int denomenator_choice = denomenators.get(nCounter).intValue();
					if(numerator_choice/denomenator_choice == 1 || numerator_choice > denomenator_choice) {
						denomenator_choice += numerator_choice;
					}
					System.out.println("-------> numerator_choice: " + numerator_choice + "; denomenator_choice: " + denomenator_choice);
					Fraction fractioN = new Fraction(numerator_choice, denomenator_choice);
					fractioN = FractionUtil.reduce(fractioN);
					
					answer_choice += fractioN.toFraction();
				}
				System.out.println("Answer Choice: " + answer_choice);
				answer_choice = "";
				nCounter++;
			}
		}
		else {
			for(Integer value: numerators) {
				int numerator_choice = value.intValue();
				int denomenator_choice = denomenators.get(nCounter).intValue();
				if(numerator_choice/denomenator_choice == 1 || numerator_choice > denomenator_choice) {
					denomenator_choice += numerator_choice;
				}
				System.out.println("-------> numerator_choice: " + numerator_choice + "; denomenator_choice: " + denomenator_choice);
				Fraction fractioN = new Fraction(numerator_choice, denomenator_choice);
				fractioN = FractionUtil.reduce(fractioN);
				
				answer_choice += fractioN.toFraction();
				nCounter++;
				System.out.println("Answer Choice: " + answer_choice);
				answer_choice = "";
			}
		}

	}

}
